home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
17 Bit Software 6: Level 6
/
17 Bit - Level 6 (1998)(Epic Marketing)[!].iso
/
quartz
/
q1082.dms
/
q1082.adf
/
src.lzh
/
Fig
/
SafeMalloc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-07-18
|
1KB
|
79 lines
#include <exec/memory.h>
#define SAFETYMARGIN 40
struct MemFrag
{
struct MemFrag *NextMemFrag;
ULONG MemFragSize;
};
STATIC struct MemFrag *MemFragList;
VOID
memcleanup()
{
struct MemFrag *MemPointer,*NextMemPointer;
if(MemFragList)
{
for(MemPointer = MemFragList ; MemPointer ; MemPointer = NextMemPointer)
{
NextMemPointer = MemPointer -> NextMemFrag;
FreeMem(MemPointer,MemPointer -> MemFragSize + sizeof(struct MemFrag));
}
}
MemFragList = NULL;
}
VOID
free(APTR *blk)
{
struct MemFrag *MemPointer,*NextMemPointer = NULL;
if(MemFragList)
{
for(MemPointer = MemFragList ; MemPointer ; MemPointer = MemPointer -> NextMemFrag)
{
if((ULONG)MemPointer + sizeof(struct MemFrag) == (ULONG)blk)
{
if(NextMemPointer)
NextMemPointer -> NextMemFrag = MemPointer -> NextMemFrag;
else
MemFragList = MemPointer -> NextMemFrag;
FreeMem(MemPointer,MemPointer -> MemFragSize + sizeof(struct MemFrag));
break;
}
NextMemPointer = MemPointer;
}
}
}
APTR
malloc(ULONG MemFragSize)
{
if(MemFragSize)
{
struct MemFrag *MemFragPointer;
MemFragSize += SAFETYMARGIN;
if(MemFragPointer = (struct MemFrag *)AllocMem(MemFragSize + sizeof(struct MemFrag),MEMF_PUBLIC|MEMF_CLEAR))
{
MemFragPointer -> NextMemFrag = MemFragList;
MemFragPointer -> MemFragSize = MemFragSize;
MemFragList = MemFragPointer;
return((APTR)((ULONG)MemFragPointer + sizeof(struct MemFrag)));
}
}
return(NULL);
}